package cn.stylefeng.guns.sys.modular.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.stylefeng.guns.sys.modular.system.mapper.ProjectDataViewMapper;
import cn.stylefeng.guns.sys.modular.system.model.params.AreaParam;
import cn.stylefeng.guns.sys.modular.system.model.params.ProDataViewParam;
import cn.stylefeng.guns.sys.modular.system.model.result.*;
import cn.stylefeng.guns.sys.modular.system.service.ProjectDataViewService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.*;
import java.util.stream.Collectors;

@Service
public class ProjectDataViewServiceImpl implements ProjectDataViewService {
    private Logger logger = LoggerFactory.getLogger(ProjectDataViewServiceImpl.class);
    @Autowired
    private ProjectDataViewMapper projectDataViewMapper;

    @Override
    public List<AreaResult> getAreasByParam(AreaParam param) {
        List<AreaResult> areaResults = projectDataViewMapper.getAreasByParam(param);
        return areaResults;
    }


    @Override
    public List<DataResult> getDataOfShopStatus(ProDataViewParam param) {
        Integer lastYear = param.getYear() - 1;
        List<DataResult> dataResultList = projectDataViewMapper.getDataOfShopStatus(param, lastYear);
        return dataResultList;
    }

    @Override
    public List<OpenDataInfoResult> getDataOfOpen(ProDataViewParam param) {
        //TODO-1-确认开业
        List<DataResult> dataResultList = projectDataViewMapper.getDataOfConfirmOpen(param);
        Map<String, Integer> hashMap = getMonthMap();
        if (CollectionUtil.isNotEmpty(dataResultList)) {
            for (DataResult data : dataResultList) {
                hashMap.put(data.getName(), data.getValue() == null ? 0 : data.getValue());
            }
        }
        List<OpenDataInfoResult> dataList = hashMap.entrySet().stream().map(e -> new OpenDataInfoResult(e.getKey(), e.getValue())).collect(Collectors.toList());

        //TODO-2-预计开业
        List<DataResult> dataOfPlanOpenResultList = projectDataViewMapper.getDataOfPlanOpen(param);
        Map<String, Integer> hashMap2 = getMonthMap();

        if (CollectionUtil.isNotEmpty(dataOfPlanOpenResultList)) {
            for (DataResult data : dataOfPlanOpenResultList) {
                hashMap2.put(data.getName(), data.getValue() == null ? 0 : data.getValue());
            }
        }
        //TODO-3 合并数据
        for (OpenDataInfoResult result : dataList) {
            result.setValue2(hashMap2.get(result.getName()));
        }
        return dataList;
    }

    private Map<String, Integer> getMonthMap() {
        Map<String, Integer> hashMap = new LinkedHashMap<String, Integer>();
        hashMap.put("01", 0);
        hashMap.put("02", 0);
        hashMap.put("03", 0);
        hashMap.put("04", 0);
        hashMap.put("05", 0);
        hashMap.put("06", 0);
        hashMap.put("07", 0);
        hashMap.put("08", 0);
        hashMap.put("09", 0);
        hashMap.put("10", 0);
        hashMap.put("11", 0);
        hashMap.put("12", 0);
        return hashMap;
    }

    @Override
    public ShopAreaResult getDataOfShopArea() {
        ShopAreaResult shopAreaResult = new ShopAreaResult();

        List<ShopAreaDataResult> list = projectDataViewMapper.getValGroupByCountry();
        List<ShopAreaDataResult> list2 = projectDataViewMapper.getValGroupByProvince();
        Map<Long, List<ShopAreaDataResult>> map = list2.stream().collect(Collectors.groupingBy(ShopAreaDataResult::getCountryId));


        Integer total = 0;
        List<ShopAreaResult.CountryData> countryDataList = new ArrayList<>();
        for (ShopAreaDataResult data : list) {
            total = total + data.getValue();
            ShopAreaResult.CountryData countryData = new ShopAreaResult.CountryData();
            countryData.setCountryId(data.getCountryId());
            countryData.setCountryName(data.getCountryIdView());
            countryDataList.add(countryData);
            List<ShopAreaResult.CountryData.Data> dataList = new ArrayList<>();
            countryData.setProvinceDataList(dataList);
            List<ShopAreaDataResult> datas = map.get(data.getCountryId());
            for (ShopAreaDataResult datass : datas) {
                ShopAreaResult.CountryData.Data data1 = new ShopAreaResult.CountryData.Data();
                data1.setProvinceId(datass.getProvinceId());
                data1.setProvinceName(datass.getProvinceIdView());
                data1.setValue(datass.getValue());
                dataList.add(data1);
            }
        }
        shopAreaResult.setTotal(total);
        shopAreaResult.setCountryDataList(countryDataList);


        return shopAreaResult;
    }

    @Override
    public List<ShopAreaDataResult> getValGroupByProvince() {
        List<ShopAreaDataResult> list2 = projectDataViewMapper.getValGroupByProvince();
        return list2;
    }

    @Override
    public List<DataResult> getFloorData() {
        List<DataResult> list = projectDataViewMapper.getFloorData();

        //过滤数据，只保留楼层是阿拉伯数字表示的
//        List<DataResult> list2 = new ArrayList<>();
//        if (CollectionUtil.isNotEmpty(list)) {
//            for (int i = 0; i < list.size(); i++) {
//                if (isNumeric(list.get(i).getName())) {
//                    list2.add(list.get(i));
//                } else {
//                    logger.info("楼层不规范数据：{},个数：{}", list.get(i).getName(), list.get(i).getValue());
//                }
//            }
//        }

        //给楼层排序-英文，负数，数字
        Map<String, Integer> map1 = new LinkedHashMap<>();
        Map<String, Integer> map2 = new LinkedHashMap<>();
        Map<String, Integer> map3 = new LinkedHashMap<>();
        if (CollectionUtil.isNotEmpty(list)) {
            for (DataResult dataResult : list) {
                if ("-".equals(dataResult.getName().substring(0, 1))) {
                    map1.put(dataResult.getName(), dataResult.getValue());
                } else if (isNumeric(dataResult.getName().substring(0, 1))) {
                    map2.put(dataResult.getName(), dataResult.getValue());
                } else {
                    //文字
                    map3.put(dataResult.getName(), dataResult.getValue());
                }
            }
        }

        //负数
        Map<String, Integer> map4 = new LinkedHashMap<>();

        //倒序输出
        ListIterator<Map.Entry<String, Integer>> i = new ArrayList<Map.Entry<String, Integer>>(map1.entrySet()).listIterator(map1.size());
        while (i.hasPrevious()) {
            Map.Entry<String, Integer> entry = i.previous();
            //   System.out.println(entry.getKey()+":"+entry.getValue());

            map4.put(entry.getKey(), entry.getValue());
        }

        //map3、map4、map2数据合并

        List<DataResult> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(map3)) {
            for (Map.Entry<String, Integer> m : map3.entrySet()) {
                DataResult data = new DataResult();
                data.setName(m.getKey());
                data.setValue(m.getValue());
                resultList.add(data);
            }
        }
        if (CollectionUtil.isNotEmpty(map4)) {
            for (Map.Entry<String, Integer> m : map4.entrySet()) {
                DataResult data = new DataResult();
                data.setName(m.getKey());
                data.setValue(m.getValue());
                resultList.add(data);
            }
        }
        if (CollectionUtil.isNotEmpty(map2)) {
            for (Map.Entry<String, Integer> m : map2.entrySet()) {

                DataResult data = new DataResult();
                data.setName(m.getKey());
                data.setValue(m.getValue());
                resultList.add(data);
            }
        }


        return resultList;
    }

    @Override
    public ProvinceDataResult getOpenDaraByProvince(ProDataViewParam param) {
        //该省所有门店数
        ProvinceDataResult provinceDataResult = projectDataViewMapper.getValByProvince(param);
        //该省下各市开业门店数
        List<CityOpenData> list = projectDataViewMapper.getOpenDaraByProvince(param);
        if (provinceDataResult != null) {
            provinceDataResult.setCityDataList(list);
        }
        return provinceDataResult;
    }

    @Override
    public List<DataResult> getShopLeaseData() {
        //门店总数
        Integer all = projectDataViewMapper.getShopNum();

        //有门店租期数据的
        List<String> data2 = projectDataViewMapper.getZlTime();

        //没有租期的门店数量
        Integer noHave = 0;

        //计算处理
        Map<Integer, Integer> map = new HashMap<>();

        Map<String, Integer> map2 = new LinkedHashMap<>();
        map2.put("待确认", 0);
        map2.put("1年以下", 0);


        if (CollectionUtil.isNotEmpty(data2)) {
            noHave = all - data2.size();
            for (String val : data2) {
                if (isNumeric(val)) {
                    double intVal = Double.parseDouble(val);
                    double zhi = (double) (intVal / 12);
                    int zheng = (int) Math.floor(zhi);

                    if (zheng > 0) {
                        if (map.get(zheng) == null) {
                            map.put(zheng, 1);
                        } else {
                            int num = map.get(zheng);
                            map.put(zheng, num + 1);
                        }
                    } else if (zheng == 0 && intVal > 0) {
                        if (map2.get("1年以下") == null) {
                            map2.put("1年以下", 1);
                        } else {
                            int num = map2.get("1年以下");
                            map2.put("1年以下", num + 1);
                        }
                    } else if (zheng == 0 && intVal == 0) {
                        if (map2.get("待确认") == null) {
                            map2.put("待确认", 1);
                        } else {
                            int num = map2.get("待确认");
                            map2.put("待确认", num + 1);
                        }
                    }
                } else {
                    logger.info("门店租期不规范数据：{}", val);
                }
            }
        }
        if (map2.get("待确认") == null) {
            map2.put("待确认", noHave);
        } else {
            int num = map2.get("待确认");
            map2.put("待确认", num + noHave);
        }

        List<DataResult> dataList = map2.entrySet().stream().map(e -> new DataResult(e.getKey(), e.getValue())).collect(Collectors.toList());

        List<DataResult> dataList2 = map.entrySet().stream().map(e -> new DataResult(e.getKey().toString(), e.getValue())).collect(Collectors.toList());

        dataList.addAll(dataList2);

        return dataList;
    }

    @Override
    public List<DataResult> getFreeLeaseData() {
        //门店总数
        Integer all = projectDataViewMapper.getShopNum();

        //有门店免租期数据的
        List<String> data2 = projectDataViewMapper.getMzTime();

        //没有租期的门店数量
        Integer noHave = 0;

        //计算处理
        Map<Integer, Integer> map = new HashMap<>();

        Map<String, Integer> map2 = new HashMap<>();

        if (CollectionUtil.isNotEmpty(data2)) {
            noHave = all - data2.size();
            for (String val : data2) {

                if (isNumeric(val)) {
                    double intVal = Double.parseDouble(val);
                    int zheng = (int) Math.floor(intVal);

                    if (zheng > 0) {
                        if (map.get(zheng) == null) {
                            map.put(zheng, 1);
                        } else {
                            int num = map.get(zheng);
                            map.put(zheng, num + 1);
                        }
                    } else if (zheng == 0 && intVal > 0) {
                        if (map2.get("1月以下") == null) {
                            map2.put("1月以下", 1);
                        } else {
                            int num = map2.get("1月以下");
                            map2.put("1月以下", num + 1);
                        }
                    } else if (zheng == 0 && intVal == 0) {
                        if (map2.get("无") == null) {
                            map2.put("无", 1);
                        } else {
                            int num = map2.get("无");
                            map2.put("无", num + 1);
                        }
                    }
                } else {
                    logger.info("门店免租期不规范数据：{}", val);
                }
            }
        }
        if (map2.get("无") == null) {
            map2.put("无", noHave);
        } else {
            int num = map2.get("无");
            map2.put("无", num + noHave);
        }

        List<DataResult> dataList = map2.entrySet().stream().map(e -> new DataResult(e.getKey(), e.getValue())).collect(Collectors.toList());

        List<DataResult> dataList2 = map.entrySet().stream().map(e -> new DataResult(e.getKey().toString(), e.getValue())).collect(Collectors.toList());

        dataList.addAll(dataList2);

        return dataList;
    }

    @Override
    public List<DataResult> getDormitoryRentData(ProDataViewParam param) {


        //区间的间隔
        int space = 5000;

        if (param.getParam() != null && param.getParam() > 0) {
            space = param.getParam();
        }

        ArrayList<Integer> list = new ArrayList<Integer>();
        //宿舍租金数据
        List<String> data = projectDataViewMapper.getDormitoryRentData();
        if (CollectionUtil.isNotEmpty(data)) {

            for (int i = 0; i < data.size(); i++) {
                if (isNumeric(data.get(i))) {
                    //进位取整
                    double intVal = Double.parseDouble(data.get(i));
                    int zheng = (int) Math.ceil(intVal);
                    list.add(zheng);
                } else {
                    logger.info("宿舍租金不规范数据：{}", data.get(i));
                }
            }
        }


        List<DataResult> resultList = funStream(list, space);
        return resultList;
    }

    @Override
    public List<DataResult> getShopRentData(ProDataViewParam param) {
        //门店数据
        LinkedHashMap<String, List<Map<String, Object>>> resultShopMap = getShopRentMapData(param);
        //区间内门店数值
        List<DataResult> resultList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(resultShopMap)) {
            for (Map.Entry<String, List<Map<String, Object>>> m : resultShopMap.entrySet()) {
                DataResult dataResult = new DataResult();
                dataResult.setName(m.getKey());
                dataResult.setValue(m.getValue().size());
                resultList.add(dataResult);
            }
        }
        return resultList;
    }

    private LinkedHashMap<String, List<Map<String, Object>>> getShopRentMapData(ProDataViewParam param) {
        List<Map<String, Object>> mapList = projectDataViewMapper.findShopRentData(param);
        BigDecimal maxAcg = BigDecimal.ZERO;

        if (CollectionUtil.isNotEmpty(mapList)) {

            for (Map<String, Object> shop : mapList) {
                BigDecimal rentSum = BigDecimal.ZERO;
                //租赁年限
                Integer years = Integer.parseInt(shop.get("zl_time").toString());
                //是否可转换为bigdecimal
                boolean areaFlag = isNumeric(shop.get("zl_area").toString());
                //租赁面积
                Double zrea = 0d;
                if (areaFlag == true) {
                    zrea = Double.parseDouble(shop.get("zl_area").toString());
                } else {
                    //面积数据不规范，跳出循环，本店数据不计算在内
                    continue;
                }
                for (int i = 1; i <= 10; i++) {
                    String rent = "rent" + i;
                    String rentValue = shop.get(rent) == null ? "0" : ("".equals(shop.get(rent)) ? "0" : shop.get(rent).toString().replace(" ", ""));
                    //是否可转换为bigdecimal
                    boolean flag = isNumeric(rentValue);
                    if (flag == false) {
                        logger.info("工程数据预览-获取门店租期数据-不规范租金数据：{}", shop.get(rent).toString());
                        //租金数据不规范，跳出循环，本店数据不计算在内
                        continue;
                    } else {
                        try {
                            BigDecimal rentVal = new BigDecimal(rentValue);
                            //System.out.println(rentVal);
                            rentSum = rentSum.add(rentVal);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }

                //分母为零不计算在内
                if (new BigDecimal(365 * years * zrea).compareTo(BigDecimal.ZERO) == 0) {
                    continue;
                }

                //计算：租金和/365/年限/面积
                BigDecimal avg = rentSum.divide(new BigDecimal(365 * years * zrea), 2, BigDecimal.ROUND_UP);
                shop.put("avg", avg);

                //获取最大值
                if (avg.compareTo(maxAcg) == 1) {
                    maxAcg = avg;
                }

            }
        }

        //按区间分组前闭后开

        //区间个数
        BigDecimal groupNum = maxAcg.divide(new BigDecimal("0.1"), 0, BigDecimal.ROUND_HALF_UP);

        //初始化区间map
        LinkedHashMap<BigDecimal, BigDecimal> resultMap = new LinkedHashMap<>();
        BigDecimal rangeNum = BigDecimal.ZERO;
        if (groupNum.compareTo(BigDecimal.ZERO) == 1) {
            for (int i = 0; i < groupNum.intValue(); i++) {
                resultMap.put(rangeNum, rangeNum.add(new BigDecimal("0.1")));
                rangeNum = rangeNum.add(new BigDecimal("0.1"));
            }
        }

        //门店数据放入区间内
        LinkedHashMap<String, List<Map<String, Object>>> resultShopMap = new LinkedHashMap<>();
        if (CollectionUtil.isNotEmpty(resultMap)) {
            for (Map.Entry<BigDecimal, BigDecimal> m : resultMap.entrySet()) {
                BigDecimal start = m.getKey();
                BigDecimal end = m.getValue();
                String rangeKey = "[" + start + "," + end + ")";
                List<Map<String, Object>> list = new ArrayList();
                resultShopMap.put(rangeKey, list);
                for (Map<String, Object> shop : mapList) {
                    if (shop.get("avg") != null) {
                        BigDecimal avg = new BigDecimal(shop.get("avg").toString());
                        if ((avg.compareTo(start) == 0 || avg.compareTo(start) == 1) && avg.compareTo(end) == -1) {
                            List<Map<String, Object>> lis = resultShopMap.get(rangeKey);
                            lis.add(shop);
                            resultShopMap.put(rangeKey, lis);
                        }
                    }
                }
            }
        }

        return resultShopMap;
    }


    @Override
    public List<Map<String, Object>> getShopRentDataByRangeKey(ProDataViewParam param) {
        //门店数据
        LinkedHashMap<String, List<Map<String, Object>>> resultShopMap = getShopRentMapData(param);
        List<Map<String, Object>> mapList = resultShopMap.get(param.getKey());
        //返回结果
        List<Map<String, Object>> mapresut = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(mapList)) {
            List<Integer> idList = new ArrayList<>();
            for (Map<String, Object> shop : mapList) {
                Integer id = Integer.parseInt(shop.get("id").toString());
                idList.add(id);
            }
            param.setParamList(idList);
            mapresut = projectDataViewMapper.getShopRentDataByRangeKey(param);
        }
        return mapresut;
    }

    @Override
    public List<Map<String, Object>> getShopRiceConsumption(ProDataViewParam param, List<Map<String, Object>> peopleQtyDayList, List<Map<String, Object>> riceDataMapList) {

        //TODO-1.大米盘亏数据
        List<Map<String, Object>> shopDataList = projectDataViewMapper.getShopRiceConsumption(param);

        //TODO-1.1 大米盘亏数据--->>大米消耗数据
        Map<String, Map<String, Object>> shopDataListMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(shopDataList)) {
            for (Map<String, Object> map : shopDataList) {
                shopDataListMap.put(map.get("shop_code").toString(), map);
            }
        }

        //TODO-1.2 就餐人数数据
        Map<String, Map<String, Object>> peopleQtyDayListMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(peopleQtyDayList)) {
            for (Map<String, Object> map : peopleQtyDayList) {
                peopleQtyDayListMap.put(map.get("shopCode").toString(), map);
            }
        }

        //TODO-1.3 外卖主食数据
        Map<String, Map<String, Object>> riceListMap = new HashMap<>();
        if (CollectionUtil.isNotEmpty(riceDataMapList)) {
            for (Map<String, Object> map : riceDataMapList) {
                riceListMap.put(map.get("shopCode").toString(), map);
            }
        }

        //TODO-2.计算百分比
        Map<String, BigDecimal> riceConsumMap = new HashMap<>();
        Map<String, BigDecimal> peopleMap = new HashMap<>();
        Map<String, BigDecimal> percentMap = new HashMap<>();

        if (CollectionUtil.isNotEmpty(peopleQtyDayListMap)) {
            for (Map.Entry<String, Map<String, Object>> m : peopleQtyDayListMap.entrySet()) {
                String shopCode = m.getKey();
                String peopleQty = m.getValue().get("peopleQty").toString();
                //就餐人数
                Integer peopleQtyNum = Integer.parseInt(peopleQty);
                //外卖专属主食
                BigDecimal riceNum = BigDecimal.ZERO;
                Map<String, Object> riceMap = riceListMap.get(shopCode);
                if (riceMap != null) {
                    riceNum = new BigDecimal(riceMap.get("riceQty").toString());
                }

                //大米消耗=大米盘亏数-外卖主食数量*8斤
                BigDecimal jin = BigDecimal.ZERO;
                Map<String, Object> map = shopDataListMap.get(shopCode);
                if (map != null) {
                    jin = new BigDecimal(map.get("jin").toString());
                    jin = jin.subtract(riceNum.multiply(new BigDecimal(8)));

                    BigDecimal bigDecimal1 = jin.divide(BigDecimal.valueOf(peopleQtyNum), 6, BigDecimal.ROUND_UP);
                    NumberFormat percent = NumberFormat.getPercentInstance();
                    percent.setMaximumFractionDigits(3);
                    String percentStr = percent.format(bigDecimal1.doubleValue());
                    m.getValue().put("jin", jin);
                    m.getValue().put("percent", percentStr);

                    //排序使用
                    //不显示非零数据
                    if (param.getShowZero() == 0) {
                        if (jin.compareTo(BigDecimal.ZERO) != 0) {
                            riceConsumMap.put(shopCode, jin);
                        }
                        if (peopleQtyNum != 0) {
                            peopleMap.put(shopCode, BigDecimal.valueOf(peopleQtyNum));
                        }
                        if (bigDecimal1.compareTo(BigDecimal.ZERO) != 0) {
                            percentMap.put(shopCode, bigDecimal1);
                        }
                    }
                    //显示非零数据
                    if (param.getShowZero() == 1) {
                        riceConsumMap.put(shopCode, jin);
                        peopleMap.put(shopCode, BigDecimal.valueOf(peopleQtyNum));
                        percentMap.put(shopCode, bigDecimal1);
                    }
                } else {
                    m.getValue().put("jin", jin);
                    m.getValue().put("percent", "0.00%");

                    //排序使用
                    //不显示非零数据
                    if (param.getShowZero() == 0) {
                        if (jin.compareTo(BigDecimal.ZERO) != 0) {
                            riceConsumMap.put(shopCode, jin);
                        }
                        if (peopleQtyNum != 0) {
                            peopleMap.put(shopCode, BigDecimal.valueOf(peopleQtyNum));
                        }
                    }
                    //显示非零数据
                    if (param.getShowZero() == 1) {
                        riceConsumMap.put(shopCode, jin);
                        peopleMap.put(shopCode, BigDecimal.valueOf(peopleQtyNum));
                        percentMap.put(shopCode, BigDecimal.ZERO);
                    }
                }
            }
        }

        //TODO-3.排序
        LinkedHashMap<String, BigDecimal> linkedHashMap = new LinkedHashMap<>();
        switch (param.getType()) {
            case 1:
                //按大米消耗排序
                linkedHashMap = sortMap(riceConsumMap, param.getLimit());
                break;
            case 2:
                //按就餐人数排序
                linkedHashMap = sortMap(peopleMap, param.getLimit());
                break;
            case 3:
                //按百分比排序-从大到小
                //LinkedHashMap<String, BigDecimal> linkedHashMapSort = sortMap(percentMap, param.getLimit());
                linkedHashMap = sortMapReverse(percentMap, param.getLimit());

//                if (CollectionUtil.isNotEmpty(linkedHashMapSort)) {
//                    //从小到大排序
//                    ListIterator<Map.Entry> i = new ArrayList<Map.Entry>(linkedHashMapSort.entrySet()).listIterator(linkedHashMapSort.size());
//                    while (i.hasPrevious()) {
//                        Map.Entry entry = i.previous();
//                        //System.out.println("key:" + entry.getKey() + ",value:" + entry.getValue());
//                        linkedHashMap.put(entry.getKey().toString(), linkedHashMapSort.get(entry.getKey().toString()));
//                    }
//                }

                break;
        }

        //TODO-4.处理返回结果
        List<Map<String, Object>> resultMap = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(linkedHashMap)) {
            for (Map.Entry<String, BigDecimal> entry : linkedHashMap.entrySet()) {
                String key = entry.getKey();
                resultMap.add(peopleQtyDayListMap.get(key));
            }
        }


        return resultMap;
    }


    @Override
    public List<Map<String, Object>> findPeopleQtyByParam(ProDataViewParam param) {
        List<Map<String, Object>> businessData = projectDataViewMapper.findPeopleQtyByParam(param);
        return businessData;
    }

    @Override
    public List<Map<String, Object>> findTakeAwayRiceDataByParam(ProDataViewParam param2) {
        List<Map<String, Object>> businessData = projectDataViewMapper.findTakeAwayRiceDataByParam(param2);
        return businessData;
    }

    /**
     * 从大到校排序
     *
     * @param map
     * @param limit
     * @return
     */
    public static LinkedHashMap<String, BigDecimal> sortMap(Map<String, BigDecimal> map, Integer limit) {
        class MapClass {                                                            //自定义类保存键值对
            private String key;
            private BigDecimal value;

            public MapClass(String key, BigDecimal value) {
                super();
                this.key = key;
                this.value = value;
            }

            public String getKey() {
                return key;
            }

            public BigDecimal getValue() {
                return value;
            }


        }
        class MapSortMethod implements Comparator<MapClass> {                    //为自定义类实现排序方法
            @Override
            public int compare(MapClass o1, MapClass o2) {
                //int result = Integer.compare(o1.getValue(), o2.getValue());        //按值大小升序排列
                int result = compareBig(o2.getValue(), o1.getValue());    //按值大小降序排列
                if (result != 0) {
                    return result;
                }
                return o1.getKey().compareTo(o2.getKey());                        //值相同时按键字典顺序排列
            }
        }

        List<MapClass> mapclass = new ArrayList<MapClass>();                //以ArrayList保存自定义类
        for (String k : map.keySet()) {
            mapclass.add(new MapClass(k, map.get(k)));
        }
        Collections.sort(mapclass, new MapSortMethod());                        //使用Collections.sort()方法，第二个参数为自定义排序方法，需要实现Comparator接口

        if (limit != null && mapclass.size() > limit) {
            mapclass = mapclass.subList(0, limit);
        }

        LinkedHashMap<String, BigDecimal> sortedMap = new LinkedHashMap<String, BigDecimal>();
        for (MapClass m : mapclass) {
            sortedMap.put(m.getKey(), m.getValue());
        }
        return sortedMap;                                                        //用LinkedHashMap返回排好序的Map
    }

    /**
     * 从小到大排序
     *
     * @param map
     * @param limit
     * @return
     */
    public static LinkedHashMap<String, BigDecimal> sortMapReverse(Map<String, BigDecimal> map, Integer limit) {
        class MapClass {                                                            //自定义类保存键值对
            private String key;
            private BigDecimal value;

            public MapClass(String key, BigDecimal value) {
                super();
                this.key = key;
                this.value = value;
            }

            public String getKey() {
                return key;
            }

            public BigDecimal getValue() {
                return value;
            }


        }
        class MapSortMethod implements Comparator<MapClass> {                    //为自定义类实现排序方法
            @Override
            public int compare(MapClass o1, MapClass o2) {
                //int result = Integer.compare(o1.getValue(), o2.getValue());        //按值大小升序排列
                int result = compareSmall(o2.getValue(), o1.getValue());    //按值大小降序排列
                if (result != 0) {
                    return result;
                }
                return o1.getKey().compareTo(o2.getKey());                        //值相同时按键字典顺序排列
            }
        }

        List<MapClass> mapclass = new ArrayList<MapClass>();                //以ArrayList保存自定义类
        for (String k : map.keySet()) {
            mapclass.add(new MapClass(k, map.get(k)));
        }
        Collections.sort(mapclass, new MapSortMethod());                        //使用Collections.sort()方法，第二个参数为自定义排序方法，需要实现Comparator接口

        if (limit != null && mapclass.size() > limit) {
            mapclass = mapclass.subList(0, limit);
        }

        LinkedHashMap<String, BigDecimal> sortedMap = new LinkedHashMap<String, BigDecimal>();
        for (MapClass m : mapclass) {
            sortedMap.put(m.getKey(), m.getValue());
        }
        return sortedMap;                                                        //用LinkedHashMap返回排好序的Map
    }

    public static int compareBig(BigDecimal x, BigDecimal y) {
        return (x.compareTo(y) == -1) ? -1 : ((x.compareTo(y) == 0) ? 0 : 1);
    }

    public static int compareSmall(BigDecimal y, BigDecimal x) {
        return (x.compareTo(y) == -1) ? -1 : ((x.compareTo(y) == 0) ? 0 : 1);
    }

    public boolean isBigDecimal(String str) {
        if (str == null || str.trim().length() == 0) {
            return false;
        }
        char[] chars = str.toCharArray();
        int sz = chars.length;
        int i = (chars[0] == '-') ? 1 : 0;
        if (i == sz) {
            return false;
        }

        if (chars[i] == '.') {
            return false;//除了负号，第一位不能为'小数点'
        }

        boolean radixPoint = false;
        for (; i < sz; i++) {
            if (chars[i] == '.') {
                if (radixPoint) {
                    return false;
                }
                radixPoint = true;
            } else if (!(chars[i] >= '0' && chars[i] <= '9')) {
                return false;
            }
        }
        return true;
    }

    /**
     * 对list按区间分组
     *
     * @param list
     * @param space
     * @return
     */
    private List<DataResult> funStream(ArrayList<Integer> list, int space) {
        // 使用流处理 把 数字分组
        Map<Integer, List<Integer>> map = list.stream().collect(Collectors.groupingBy((num) -> {
            return num / space;// 分组规则
        }));

        // 获取集合里的最大值
        Optional<Integer> maxop = list.stream().collect(Collectors.maxBy(Integer::compareTo));
        int max = maxop.orElse(0);
        // 计算出区间的数量
        int groups = max % space == 0 ? max / space : max / space + 1;
        // 结果
        List<DataResult> dataResults = new ArrayList<>();
        for (int i = 0; i < groups; i++) {
            DataResult result = new DataResult();
            result.setName(i * space + "~" + (i + 1) * space);
            result.setValue(map.get(i) == null ? 0 : map.get(i).size());
            dataResults.add(result);
        }
        return dataResults;
    }


    /**
     * 判断是否为数字
     *
     * @param str
     * @return
     */
    public static boolean isNumeric(final String str) {
        if (str == null || str.length() == 0) {
            return false;
        }

        try {
            Integer.parseInt(str);
            return true;
        } catch (NumberFormatException e) {
            try {
                Double.parseDouble(str);
                return true;
            } catch (NumberFormatException ex) {
                try {
                    Float.parseFloat(str);
                    return true;
                } catch (NumberFormatException exx) {
                    return false;
                }
            }
        }
    }

}
